package com.aj.activity.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.aj.activity.R;
import com.aj.databases.entry.Family;

public class ContantAdapter extends BaseAdapter {
	private Context context;
	private List<Family> familyList;
	private HashMap<Integer, View> viewCache = new HashMap<Integer, View>();
	private List<Family> chooseFamilyList = new ArrayList<Family>();

	public ContantAdapter(Context context, List<Family> familyList) {
		this.context = context;
		this.familyList = familyList;
		chooseFamilyList.clear();
	}

	@Override
	public int getCount() {
		return familyList.size();
	}

	@Override
	public Object getItem(int position) {
		return familyList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public class ViewHolder {
		public CheckBox choose_user;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		convertView = viewCache.get(position);
		final Family family = familyList.get(position);
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_contant, null);
			viewHolder = new ViewHolder();
			viewHolder.choose_user = (CheckBox) convertView
					.findViewById(R.id.choose_user);
			viewCache.put(position, convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.choose_user.setText(family.getCallName());
		viewHolder.choose_user
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							chooseFamilyList.add(family);
						} else {
							chooseFamilyList.remove(family);
						}
						notifyDataSetChanged();
					}
				});
		return convertView;
	}

	public List<Family> getChooseFamilyList() {
		return chooseFamilyList;
	}

	public void setChooseFamilyList(List<Family> chooseFamilyList) {
		this.chooseFamilyList = chooseFamilyList;
	}

}
